<template>
  <div>
    <el-row>
      <el-col :span="6" style="padding-right:.5em;border-right:2px solid #f4f4f4">
        <el-tree :data="eds" :props="defaultProps" @node-click="handleNodeClick" />
      </el-col>
      <el-date-picker
        v-model="value1"
        type="datetime"
        placeholder="选择日期时间"
      />
      <el-col :span="18">
        <div v-if="params.device_id">
          <el-table
            :data="monitor"
            style="width: 100%"
          >
            <el-table-column
              prop="d"
              label="日期"
              width="100"
            />
            <el-table-column
              prop="tsp"
              label="tsp"
              width="100"
            />
            <el-table-column
              prop="pm10"
              label="pm10"
              width="100"
            />
            <el-table-column
              prop="pm25"
              label="pm2.5"
              width="100"
            />
            <el-table-column
              prop="temperature"
              label="温度"
              width="100"
            />
            <el-table-column
              prop="humidity"
              label="湿度"
              width="100"
            />
            <el-table-column
              prop="wind_direction"
              label="风向"
              width="140"
            />
            <el-table-column
              prop="wind_speed"
              label="风速"
              width="100"
            />
          </el-table>
        </div>
        <div v-else style="line-height:3em;color:red;text-align:center">请选择设备</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { get } from '@/utils/request'
export default {
  // 变量
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      eds: [],
      params: {},
      monitor: {}
    }
  },
  // 页面加载完毕后执行
  created() {
    this.loadEngineerDevices()
  },
  // 方法
  methods: {
    handleNodeClick(node) {
      if (node.engineer_id) {
        // 点击了设备
        this.$set(this.params, 'engineer_id', node.engineer_id)
        this.$set(this.params, 'device_id', node.id)
        // 参数重组
        const paramss = {
          engineer_id: node.engineer_id,
          device_id: node.id
        }
        // 请求月数据
        get('/monitor/queryMouthData', paramss).then(resp => {
          this.monitor = resp.data
        })
      }
    },
    // 加载工程设备树
    loadEngineerDevices() {
      const url = '/engineer/findEngineerDeviceTree'
      get(url).then(resp => {
        this.eds = resp.data
      })
    }
  }
}
</script>
